﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace AttributeReflect
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "利用晚期绑定反射特性";

//加载指定路径上的程序集
Assembly myAssembly = Assembly.LoadFile("V:\\MyAssembly.dll");
Type attributeType = myAssembly.GetType("MyAssembly.MyOwnAttribute");//获取自定义特性
PropertyInfo nameInfo = attributeType.GetProperty("Name");//获取特性中的Name属性信息
foreach (var myOwnClassType in myAssembly.GetTypes())//遍历程序集中所有的类型
{
    //获取附加在类型上的自定义特性实例
    object[] attributes = myOwnClassType.GetCustomAttributes(attributeType, true);
    if (attributes.Length > 0)
    {
        Console.WriteLine("类型 {0} 具有特性 {1}", myOwnClassType.Name, attributeType.Name);
        foreach (var myOwnClassAttribute in attributes)
        {
            Console.WriteLine("\t特性Name属性为：{0}", nameInfo.GetValue(myOwnClassAttribute, null));
        }
    }
    else
    {
        Console.WriteLine("类型 {0} 不具有特性 {1}", myOwnClassType.Name, attributeType.Name);
    }
    foreach (var method in myOwnClassType.GetMethods())
    {
        //获取附加在方法上的自定义特性实例
        object[] methodAttributes = method.GetCustomAttributes(attributeType, true);
        if (methodAttributes.Length > 0)
        {
            Console.WriteLine("\t方法 {0} 具有特性 {1}", method.Name, attributeType.Name);
            foreach (var myOwnClassAttribute in methodAttributes)
            {
                Console.WriteLine("\t\t特性Name属性为：{0}", nameInfo.GetValue(myOwnClassAttribute, null));
            }
        }
    }
}

            Console.Read();
        }
    }
}
